﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.Weather.Interfaces;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace DispatchAssist.Weather.Helper
{
    class AirportHelper
    {
        //最西边：73.50239753723144
        //最北边: 53.56355951022927
        //最东边：135.09578704833984
        //最南边: 18.154660251578434

        //东西：61.59338951110836
        //南北: 35.40889925865084

        //0.009655952455  = 1000m

        public static IList<IAirportLocation> AirportLocations { get { return WeatherDataHelper.WeatherData.Airports; } }


        public static IList<IAirportLocation> GetDisplayAirports(IWeatherMapInfo info)
        {
            var returnLocations = new List<IAirportLocation>();
            foreach (var airport in AirportLocations)
            {
                if (airport.LocationX > info.BeginLocationX && airport.LocationX < info.EndLocationX
                    && airport.LocationY < info.BeginLocationY && airport.LocationY > info.EndLocationY)
                {
                    returnLocations.Add(airport);
                }
            }
            return returnLocations;
        }

        public static void DrawAirport(PaintEventArgs e, decimal zoomScale, IWeatherMapInfo info, IAirportLocation airLocation, decimal left, decimal top)
        {
            SolidBrush mybrush = new SolidBrush(Properties.UserSettings.Default.AirportColor);
            Font myfont = new Font("黑体", 9); //设置字体类型及颜色
            CarolLib.Log.Instance.Info((airLocation.LocationX - info.BeginLocationX) / info.ScaleX);
            var x = (((airLocation.LocationX - info.BeginLocationX) / info.ScaleX) + info.LocationX) * zoomScale + left;
            var y = (((info.BeginLocationY - airLocation.LocationY) / info.ScaleY) + info.LocationY) * zoomScale + top;
            CarolLib.Log.Instance.Info(string.Format("写入机场：{0}, 颜色:{1},[{2},{3}]", airLocation.Name, Properties.UserSettings.Default.AirportColor, x, y));
            //调用DrawString方法在图像上写文字
            e.Graphics.DrawEllipse(new Pen(mybrush), new Rectangle((int)x, (int)y, 2, 2));
            e.Graphics.DrawString(airLocation.Name, myfont, mybrush,
                 new Rectangle((int)x, (int)y - 6, 100, 12));
            //  e.ClipRectangle

        }

        ////如果原图片是索引像素格式之列的，则需要转换
        //       if (IsPixelFormatIndexed(img.PixelFormat))
        //       {
        //           Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
        //           using (Graphics g = Graphics.FromImage(bmp))
        //           {
        //               g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        //               g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //               g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //               g.DrawImage(img, 0, 0);
        //           }

        //           System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
        //           System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(bmp);
        //           int x = (int)Convert.ToInt32(bmp.Width * xPer);
        //           int y = (int)Convert.ToInt32(bmp.Height * yPer);

        //           g2.DrawImage(copyImage, new System.Drawing.Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        //           bmp.Save(Path_syp);


        //           g2.Dispose();
        //           bmp.Dispose();

        //       }
        //       else
        //       {
        //           System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        //           System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
        //           System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        //           int x = (int)Convert.ToInt32(image.Width * xPer);
        //           int y = (int)Convert.ToInt32(image.Height * yPer);

        //           g.DrawImage(copyImage, new System.Drawing.Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        //           image.Save(Path_syp);


        //           g.Dispose();
        //           image.Dispose();
        //       }



        /// <summary>
        /// 会产生graphics异常的PixelFormat
        /// </summary>
        private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare, PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed, PixelFormat.Format8bppIndexed };

        /// <summary>
        /// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
        /// 无法从带有索引像素格式的图像创建graphics对象
        /// </summary>
        /// <param name="imgPixelFormat">原图片的PixelFormat</param>
        /// <returns></returns>
        private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
        {
            foreach (PixelFormat pf in indexedPixelFormats)
            {
                if (pf.Equals(imgPixelFormat)) return true;
            }

            return false;
        }



    }
}
